package com.shu.data;

import org.junit.Test;

import java.util.*;

public class SequenceMap {


    @Test
    public void testLinkedMap() {

        //LinkedHashMap 有序
        Map maps = new LinkedHashMap();
        maps.put("1", "张三");
        maps.put("2", "李四");
        maps.put("4", "赵六");
        maps.put("3", "王五");
        System.out.println("LinkedHashMap(有序):");
        Iterator it = maps.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entity = (Map.Entry) it.next();
            System.out.println("[ key = " + entity.getKey() +
                    ", value = " + entity.getValue() + " ]");
        }


        //HashMap 无序
        Map map = new HashMap();
        map.put("1", "张三");
        map.put("3", "王五");
        map.put("2", "李四");
        map.put("4", "赵六");
        map.put(null, "nil");
        map.put("", "---");
        it = null;
        System.out.println("HashMap(无序):");
        it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entity = (Map.Entry) it.next();
            System.out.println("[ key = " + entity.getKey() +
                    ", value = " + entity.getValue() + " ]");
        }


        System.out.println("TreeMap(有序):");
        TreeMap<Object, Object> treeMap = new TreeMap<>();
        treeMap.put("4", "赵六");
        treeMap.put("2", "李四");
        treeMap.put("1", "张三");
        treeMap.put("3", "王五");
//        treeMap.put(null, "nil");
        treeMap.put("9", "---");
        treeMap.put("a", "---");
        treeMap.put("aA", "---");
        treeMap.put("b", "---");

        treeMap.forEach((key, v) -> {
            System.out.println(key + "\t" + v);
        });
    }
}
